home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / Jotto2 src / Jotto ][ ƒ / Jotto code ƒ / jotto load-save.c < prev    next >
Text File  |  1993-12-11  |  5KB  |  206 lines

  1. /**********************************************************************\
  2.  
  3. File:        jotto load-save.c
  4.  
  5. Purpose:    This module handles loading and saving games on disk.
  6.  
  7.  
  8. Jotto ][ -=- a simple word game, revisited
  9. Copyright (C) 1993 Mark Pilgrim
  10.  
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15.  
  16. This program is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. GNU General Public License for more details.
  20.  
  21. You should have received a copy of the GNU General Public License
  22. along with this program in a file named "GNU General Public License".
  23. If not, write to the Free Software Foundation, 675 Mass Ave,
  24. Cambridge, MA 02139, USA.
  25.  
  26. \**********************************************************************/
  27.  
  28. #include "jotto globals.h"
  29. #include "jotto error.h"
  30. #include "jotto load-save.h"
  31. #include "jotto files.h"
  32. #include "jotto.h"
  33. #include "msg dialogs.h"
  34. #include "msg environment.h"
  35. #include "util.h"
  36. #include "Script.h"
  37.  
  38. FSSpec            gameFile;
  39. Boolean            deleteTheThing;
  40.  
  41. typedef struct
  42. {
  43.     unsigned int    version;                    /* for forward compatibility */
  44.     unsigned char    numLetters;                    /* 5 or 6 */
  45.     unsigned char    computerWord[6];            /* encrypted! */
  46.     unsigned char    numRight[MAX_TRIES];        /* # letters right in n-th guess */
  47.     unsigned char    humanWord[MAX_TRIES][6];    /* n-th guess */
  48.     unsigned char    numTries;                    /* # guesses so far */
  49.     unsigned char    whichChar;                    /* which char 1-6 of current word */
  50.     unsigned char    checksum;                    /* checksum of previous data */
  51. } SaveStruct;
  52.  
  53. void InitLoadSave(void)
  54. {
  55.     gameFile.name[0]=0x00;
  56.     deleteTheThing=FALSE;
  57. }
  58.  
  59. void LoadSaveDispatch(Boolean isLoad, Boolean useOldGame)
  60. {
  61.     if (isLoad)
  62.     {
  63.         if (GetSourceFile(&gameFile, useOldGame))
  64.             HandleError(GetSavedGame(&gameFile));
  65.     }
  66.     else
  67.     {
  68.         useOldGame=useOldGame&&(gameFile.name[0]!=0x00);
  69.         if (useOldGame ? TRUE : GetDestFile(&gameFile, &deleteTheThing))
  70.             HandleError(SaveGame(gameFile));
  71.     }
  72. }
  73.  
  74. int SaveGame(FSSpec gameFile)
  75. {
  76.     OSErr            theError;
  77.     int                thisFile;
  78.     long            count;
  79.     SaveStruct        data;
  80.     unsigned char    checksum;
  81.     int                i,j;
  82.     
  83.     data.version=SAVE_VERSION;
  84.     data.numLetters=gNumLetters;
  85.     for (i=0; i<6; i++)
  86.         data.computerWord[i]=gComputerWord[i]^0x42;
  87.     for (i=0; i<MAX_TRIES; i++)
  88.         data.numRight[i]=gNumRight[i];
  89.     for (i=0; i<MAX_TRIES; i++)
  90.         for (j=0; j<6; j++)
  91.             data.humanWord[i][j]=gHumanWord[i][j];
  92.     data.numTries=gNumTries;
  93.     data.whichChar=gWhichChar;    
  94.     checksum=0;
  95.     for (i=0; i<sizeof(SaveStruct)-2; i++)
  96.         checksum+=*((unsigned char*)((long)&data+i));
  97.     data.checksum=checksum;
  98.     
  99.     if (deleteTheThing)
  100.     {
  101.         if (gHasFSSpecs)
  102.             FSpDelete(&gameFile);
  103.         else
  104.             HDelete(gameFile.vRefNum, gameFile.parID, gameFile.name);
  105.     }
  106.     FlushVol(0L, gameFile.vRefNum);
  107.     
  108.     if (gHasFSSpecs)
  109.         theError=FSpCreate(&gameFile, CREATOR, SAVE_TYPE, smSystemScript);
  110.     else
  111.         theError=HCreate(gameFile.vRefNum, gameFile.parID,
  112.                         gameFile.name, CREATOR, SAVE_TYPE);
  113.     FlushVol(0L, gameFile.vRefNum);
  114.     
  115.     if (theError!=noErr)
  116.         return kCantCreateGame;
  117.     
  118.     if (gHasFSSpecs)
  119.         theError=FSpOpenDF(&gameFile, fsRdWrPerm, &thisFile);
  120.     else
  121.         theError=HOpen(gameFile.vRefNum, gameFile.parID,
  122.                         gameFile.name, fsRdWrPerm, &thisFile);
  123.     
  124.     if (theError!=noErr)
  125.         return kCantOpenGameToSave;
  126.         
  127.     count=sizeof(SaveStruct);
  128.     SetEOF(thisFile, count);
  129.     SetFPos(thisFile, 1, 0L);
  130.     theError=FSWrite(thisFile, &count, &data);
  131.     FSClose(thisFile);
  132.     FlushVol(0L, gameFile.vRefNum);
  133.  
  134.     if (theError!=noErr)
  135.     {
  136.         if (gHasFSSpecs)
  137.             FSpDelete(&gameFile);
  138.         else
  139.             HDelete(gameFile.vRefNum, gameFile.parID, gameFile.name);
  140.         gameFile.name[0]=0x00;
  141.         return kCantWriteGame;
  142.     }
  143.     else deleteTheThing=TRUE;
  144.     
  145.     return allsWell;
  146. }
  147.  
  148. int GetSavedGame(FSSpec *gameFile)
  149. {
  150.     int                thisFile;
  151.     unsigned char    checksum;
  152.     long            count;
  153.     int                i,j;
  154.     SaveStruct        data;
  155.     OSErr            theError;
  156.  
  157.     if (gHasFSSpecs)
  158.         theError=FSpOpenDF(gameFile, fsRdPerm, &thisFile);
  159.     else
  160.         theError=HOpen(gameFile->vRefNum, gameFile->parID, gameFile->name, fsRdPerm, &thisFile);
  161.     
  162.     if (theError!=noErr)
  163.         return kCantOpenGameToLoad;
  164.     
  165.     count=sizeof(SaveStruct);
  166.     SetFPos(thisFile, 1, 0L);
  167.     theError=FSRead(thisFile, &count, &data);
  168.     FSClose(thisFile);
  169.  
  170.     if (theError!=noErr)
  171.         return kCantLoadGame;
  172.     
  173.     deleteTheThing=TRUE;
  174.     
  175.     if (data.version!=SAVE_VERSION)
  176.         return kSaveVersionNotSupported;
  177.     
  178.     checksum=0;
  179.     for (i=0; i<sizeof(SaveStruct)-2; i++)
  180.             checksum+=*((unsigned char*)((long)&data+i));
  181.  
  182.     if (checksum!=data.checksum)
  183.     {
  184.         gameFile->name[0]=0x00;
  185.         return kBadChecksum;
  186.     }
  187.  
  188.     gNumLetters=data.numLetters;
  189.     for (i=0; i<6; i++)
  190.         gComputerWord[i]=data.computerWord[i]^0x42;
  191.     for (i=0; i<15; i++)
  192.         gNumRight[i]=data.numRight[i];
  193.     for (i=0; i<15; i++)
  194.         for (j=0; j<6; j++)
  195.             gHumanWord[i][j]=data.humanWord[i][j];
  196.     gNumTries=data.numTries;
  197.     gWhichChar=data.whichChar;
  198.     if (gNumTries==MAX_TRIES)
  199.         Mymemcpy((Ptr)gHumanWord[gNumTries], (Ptr)gComputerWord, 6);
  200.     
  201.     gInProgress=(gNumRight[gNumTries]!=gNumLetters) && (gNumTries<MAX_TRIES);
  202.     StartGame();
  203.     
  204.     return allsWell;
  205. }
  206.